package com.hotel.front;

import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.SourcesBusinessLogic;

import data.SourcesBusiness;
import data.passingData.ResultData;


@SuppressWarnings("serial")
public class SourcesBusinessService extends HttpServlet{
	//http://localhost:8080/sourcesbusinessService?func=addSourcesBusiness&title=Yahoo&description=Yahoo&defaultsource=true
	//http://localhost:8080/sourcesbusinessService?func=addSourcesBusiness&title=OnlineReservation&description=OnlineReservation&defaultsource=false
	//http://localhost:8080/sourcesbusinessService?func=addSourcesBusiness&title=GuestReferral&description=GuestReferral&defaultsource=false
	//http://localhost:8080/sourcesbusinessService?func=addSourcesBusiness&title=ReturnGuest&description=ReturnGuest&defaultsource=false
	//http://localhost:8080/sourcesbusinessService?func=addSourcesBusiness&title=Google&description=Google&defaultsource=false
	//http://localhost:8080/sourcesbusinessService?func=editSourcesBusiness&sourcesbusinessId=873&title=12345&description=aabbb
	//http://localhost:8080/sourcesbusinessService?func=changeStatus&sourcesbusinessId=873
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("addSourcesBusiness")){
				AddSourcesBusiness(req,resp);
			}else if(doClass.equals("editSourcesBusiness")){
				EditSourcesBusiness(req,resp);
			}else if(doClass.equals("delSourcesBusiness")){
				DeleteSourcesBusiness(req,resp);
			}else if(doClass.equals("changeStatus")){
				ChangeStatus(req,resp);
			}else if(doClass.equals("selectSourceBuss")){
				SelectSourceBuss(req,resp);
			}else if(doClass.equals("getAllSourcesBusiness4Admin")){
				GetAllSourcesBusiness4Admin(req,resp);
			}else if(doClass.equals("getSourcesBusinessById")){
				GetSourcesBusinessById(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void AddSourcesBusiness(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}	
		long hotelId = (Long) session.getAttribute("hotelId");
		String title = req.getParameter("title");
		String description = req.getParameter("description");
		Boolean defaultSource = Boolean.valueOf(req.getParameter("defaultsource"));
		
		new SourcesBusinessLogic().AddSourcesBusiness(hotelId,title,description,defaultSource);
		
		GetAllSourcesBusiness4Admin(req, resp);
	}

	private void EditSourcesBusiness(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long sourcesbusinessId = Long.parseLong(req.getParameter("sourcesbusinessId"));
		String title = req.getParameter("title");
		String description = req.getParameter("description");
				
		new SourcesBusinessLogic().EditSourcesBusiness(sourcesbusinessId,title,description);

		GetAllSourcesBusiness4Admin(req, resp);
	}
	
	private void DeleteSourcesBusiness(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		String id = req.getParameter("id");
		
		for(int i = 0; i < id.split(",").length ; i++){
			new SourcesBusinessLogic().DeleteSourcesBusiness(Long.parseLong(id.split(",")[i]));
		}
		
		GetAllSourcesBusiness4Admin(req, resp);
	}
	
	private void ChangeStatus(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long sourcesbusinessId = Long.parseLong(req.getParameter("sourcesbusinessId"));

		Object obj = new SourcesBusinessLogic().ChangeStatus(sourcesbusinessId);

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void SelectSourceBuss(HttpServletRequest req, HttpServletResponse resp) throws IOException, ParseException
	{
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = 1;
		if(session.getAttribute("hotelId") != null)
			hotelId = (Long)session.getAttribute("hotelId");
		List<SourcesBusiness> lsitBuss = new SourcesBusinessLogic().GetSelectBoxSourcesBusiness(hotelId);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(lsitBuss));
	}
	
	private void GetAllSourcesBusiness4Admin(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		long offset = Long.parseLong(req.getParameter("offset"));
		long line = Long.parseLong(req.getParameter("line"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		
		List<SourcesBusiness> obj = new SourcesBusinessLogic().GetAllSourcesBusiness4AdminLimit(hotelId,offset,line);
		double dataSize = new SourcesBusinessLogic().GetAllSourcesBusiness4Admin(hotelId).size();
		
		Gson gson = new Gson();
		
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("dataAmount",dataSize);
		data.put("pageNumber",pointer);
		data.put("lineAmount", line);
		data.put("pageAmount", Math.ceil(dataSize/line));
		data.put("data", obj);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}
	
	private void GetSourcesBusinessById(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		long id = Long.parseLong(req.getParameter("id"));
		SourcesBusiness c = new SourcesBusinessLogic().GetSourcesBusiness(id);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(c));
	}

}
